<?php
//No direct access
defined('_JEXEC') or die('Restricted access');

jimport('joomla.application.component.model');

class OwnserviceModelLevel extends JModel
{
	/**
	 * Level data
	 *
	 * @var object
	 */
	var $_data = null;

	/**
	 * Constructor
	 *
	 * @since 1.5
	*/
	function __construct()
	{
		parent::__construct();

		$id = JRequest::getVar('id');
		$this->setId((int)$id);
	}

	/**
	 * Method to set the level id
	 *
	 * @access	public
	 * @param	int	level ID number
	 */
	function setId($id) 
	{
		// Set new level ID and wipe data
		$this->_id		= $id;
		$this->_data	= null;
	}

	/**
	 * Overridden get method to get properties level the data
	 *
	 * @access	public
	 * @param	string	$property	The name of the property
	 * @param	mixed	$value		The value of the property to set
	 * @return 	mixed 				The value of the property
	 * @since	1.0
	 */
	function get($property, $default=null) 
	{
		/*
		* Load the level data
		*/
		if ($this->_loadData()) 
		{
			if(isset($this->_data->$property)) 
			{
				return $this->_data->$property;
			}
		}
		return $default;
	}

	/**
	 * Method to get level data
	 *
	 * @access public
	 * @return object
	*/
	function getItem()
	{
		/*
		* Load the fee data
		*/
		if($this->_loadData()) 
		{
			// Someting code
		} 
		else 
		{

			$row = new stdClass();
			$row->id			= null;
			$row->from			= null;
			$row->to			= null;
			$row->title			= null;

			$this->_data = $row;
		}

		return $this->_data;
	}

	/**
	 * Method to load required data
	 *
	 * @access	private
	 * @return	array
	 * @since	1.0
	*/
	function _loadData() 
	{

		//Lets load the level if it doesn't already exist
		if(empty($this->_data)) 
		{
			$query = ' SELECT * FROM #__ownservice_levels '
					.' WHERE id='.$this->_id;

			$this->_db->setQuery($query);
			//echo $this->_db->_sql;
			$this->_data = $this->_db->loadObject();

			return (boolean) $this->_data;
		}

		return true;
	}

	/**
	 * Method to store the level
	 *
	 * @access public
	 * @return boolean True on success
	*/
	function store($data) 
	{
		//Check the request forgeries
		JRequest::checkToken() or jexit('Invalid Token');

		$row = & $this->getTable( 'ownservice_level','' );

		if(!$row->bind($data)) {
			$this->setError( $this->_db->getErrorMsg() );
			return false;
		}

		// Make sure the data is valid
		if (!$row->check()) {
			$this->setError($row->getError() );
			return false;
		}

		// Store it in the db
		if (!$row->store()) {
			$this->setError( $this->_db->getErrorMsg() );
			return false;
		}

		$this->_data = $row;

		return true;
	}
}